home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 April: Mac OS SDK / Dev.CD Apr 98 SDK1.toast / Development Kits (Disc 1) / AIAT 1.0.1 / Headers / Index / TermIndex.h < prev    next >
Encoding:
Text File  |  1997-09-11  |  10.3 KB  |  324 lines  |  [TEXT/CWIE]

  1. // TermIndex.h
  2. //    Copyright:    © 1994 - 1996 by Apple Computer, Inc., all rights reserved.
  3.  
  4. //// TermIndex: a base class for both vector and inverted indices.
  5. //// Maintains a dictionary of terms.
  6.  
  7. #pragma once
  8. #ifndef TermIndex_h
  9. #define TermIndex_h
  10.  
  11. #pragma import on
  12.  
  13. #include "IAIndex.h"
  14.  
  15. //#pragma IA_BEGIN_IMPORTS
  16. #include <time.h>
  17. //#pragma IA_END_IMPORTS
  18.  
  19. #pragma IA_BEGIN_EXPORTS
  20.  
  21. typedef uint32        TermID;
  22. typedef uint32        TermFreq;
  23. //typedef uint32    TermOffset;
  24.  
  25. typedef TermID        DocID;
  26. typedef TermFreq    DocLength;
  27.  
  28.  
  29. const uint32        TermIndexType = 'Ter2';
  30.  
  31. //// TermInfo: statistics about a term's occurrence.
  32.  
  33. // Subclasses augment, in particular to store postings, forming an inverted index.
  34. class TermInfo : public IAOrderedStorable {
  35. public:
  36.                 TermInfo();                        // term = NULL;
  37.                 TermInfo(IATerm* t, TermID i) : term(t), id(i), docCount(0) {}
  38.     IA_INLINE    ~TermInfo() IA_INLINE_DEF_BODY(delete term)
  39.  
  40.  
  41.     // IAOrderedStorable methods
  42.     IAStorable*    DeepCopy() const;
  43.     IABlockSize    StoreSize() const;
  44.     void        Store(IAOutputBlock* output) const;
  45.     IAStorable*    Restore(IAInputBlock* input) const;
  46.     bool        LessThan(const IAOrderedStorable* other) const;    // return term->LessThanNonVirtual(other->term);
  47.     bool        Equal(const IAOrderedStorable* other) const;    // return term->EqualNonVirtual(other->term);
  48.     IATerm*        GetTerm() const {return term;}
  49.     void        SetTerm(IATerm*     t) {term = t;}
  50.     TermID        GetTermID() const {return id;}
  51.     void        SetDocumentCount(TermFreq dCount) {docCount = dCount;}
  52.     TermFreq    GetDocumentCount() const {return docCount;}
  53.     
  54. protected:
  55.     void        DeepCopying(const IAStorable* source);
  56.     void        Restoring(IAInputBlock* input, const IAStorable* proto);
  57. private:
  58.                 TermInfo(TermInfo&);                // don't define a copy constructor
  59.                 
  60.     IATerm*     term;                            // the term
  61.     TermID         id;                                // its ID
  62.     TermFreq    docCount;                        // the number of docs it occurs in
  63.  
  64. };
  65.  
  66. //// DocInfo: statistics about a document.
  67.  
  68. // Subclasses augment, in particular to store vectors.
  69. class DocInfo : public IAOrderedStorable {
  70. public:
  71.                 DocInfo() : doc(NULL) {}
  72.                 DocInfo(IADoc* d, DocID i) : doc(d), id(i), length(0) {}
  73.     IA_INLINE    ~DocInfo() IA_INLINE_DEF_BODY(delete doc)
  74.  
  75.  
  76.     // IAOrderedStorable methods
  77.     IAStorable*    DeepCopy() const;
  78.     IABlockSize    StoreSize() const;
  79.     void        Store(IAOutputBlock* output) const;
  80.     IAStorable*    Restore(IAInputBlock* input) const;
  81.     bool        LessThan(const IAOrderedStorable* other) const;    // return doc->LessThan(other->doc);
  82.     bool        Equal(const IAOrderedStorable* other) const;    // return doc->Equal(other->doc);
  83.     IADoc*        GetDocument() const {return doc;}
  84.     DocID        GetDocID() const {return id;}
  85.     void        SetDocumentLength(DocLength dlength) {length = dlength;}
  86.     DocLength    GetDocumentLength() const {return length;}
  87.     void        SetDocument(IADoc*        d) {doc = d;}
  88. protected:
  89.     void        DeepCopying(const IAStorable* source);
  90.     void        Restoring(IAInputBlock* input, const IAStorable* proto);
  91. private:
  92.                 DocInfo(DocInfo&);            // don't define a copy constructor
  93.                 
  94.     IADoc*        doc;                        // the document
  95.     DocID         id;                            // its ID
  96.     DocLength    length;                        // the number of terms in the doc
  97.                 
  98. };
  99.  
  100. // internal types needed for private member declarations
  101. struct FreqPosting;
  102. class TFVector;
  103. class TFMap;
  104. class Progress;
  105. class TermUpdateSet;
  106. class DocUpdateSet;
  107.  
  108. // flush progress report support
  109. typedef void FlushProgressFn(float percent, void* data);
  110.  
  111. //// TermIndex: base class for vector & inverted indices
  112.  
  113. class TermIndex : public IAIndex {
  114.     friend class IAQuery;                // for GetTFMaps()
  115. public:
  116.             TermIndex(IAStorage* s, IACorpus* c, IAAnalysis* a,
  117.                       uint32 t = TermIndexType, IABlockID r = NULL);
  118.             ~TermIndex();
  119.     
  120.     // IAIndex methods we'll modify
  121.     void                Initialize();
  122.     void                Open();
  123.  
  124.     void                Flush();
  125.  
  126.     void                AddDoc(IADoc* doc);
  127.     void                AddDoc(IADoc* doc, DocID* returnID);
  128.     void                RenameDoc(const IADoc* oldName, const IADoc* newName);
  129.     void                DeleteDoc(const IADoc* doc);
  130.  
  131.     bool                IsDocIndexed(const IADoc* doc);
  132.  
  133.     IADocIterator*        GetDocIterator();
  134.     IADocIterator*        GetDocIterator(const IADoc* start);
  135.  
  136.     void                Merge(IAIndex** indices, uint32 indexCount);
  137.  
  138.     // TermIndex-specific methods
  139.     
  140.     virtual void    Purge();
  141.     // TermInfo's & ID's
  142.     virtual TermInfo*            GetTermInfo(IATerm* term);
  143.  
  144.     IATerm*                GetIDTerm(TermID id);
  145.     TermID                GetMaxTermID();
  146.     TermFreq            GetTermCount();
  147.  
  148.     IAOrderedStorableIterator*    GetTermInfoIterator();
  149.     IAOrderedStorableIterator*    GetTermInfoIterator(IATerm* start);
  150.  
  151.     // DocInfo's & ID's
  152.     DocInfo*            GetDocInfo(const IADoc* doc, bool noError = false);
  153.  
  154.     IADoc*                GetIDDoc(DocID id);
  155.     DocID                GetMaxDocID();
  156.     DocID                GetDocCount();
  157.  
  158.     IAOrderedStorableIterator*    GetDocInfoIterator();
  159.     IAOrderedStorableIterator*    GetDocInfoIterator(const IADoc* start);
  160.     
  161.     // Called under AddDoc(), DeleteDoc() and Flush().
  162.     void SetFlushProgressFn(FlushProgressFn* fn) {flushProgressFn = fn;}
  163.     FlushProgressFn*    GetFlushProgressFn() const {return flushProgressFn;}
  164.     void SetFlushProgressData(void* pdata) {flushProgressData = pdata;}
  165.     void*    GetFlushProgressData() const {return flushProgressData;}
  166.     void SetFlushProgressFreq(clock_t freq) {flushProgressFreq = freq;}
  167.     clock_t    GetFlushProgressFreq() const {return flushProgressFreq;}
  168.     
  169.     
  170.  
  171.  
  172.     IADefineNarrowMethods(TermIndex, IAIndex);    // support for IANarrow
  173.  
  174.     bool Validate(bool verbose = true, bool justDocs = true);        // validate this inddex
  175.     virtual bool ValidateTermInfos(bool verbose);
  176.     virtual bool ValidateDocInfos(bool verbose);
  177.     
  178.     virtual uint32        GetNorm();
  179. protected:
  180.     void                Initializing();
  181.     // methods which add some data to the index root block
  182.     IABlockSize            RootSize();
  183.     void                StoreRoot(IAOutputBlock* output);
  184.     void                RestoreRoot(IAInputBlock* input);
  185.     // To be augmented by subclasses:
  186.     //  . for inverted indices:
  187.     virtual TermInfo*    MakeTermInfo(IATerm* term, TermID id);
  188.     virtual TermInfo*    UpdateTermInfo(TermInfo* i, FreqPosting* adds, TermFreq addCount, TermFreq delCount);
  189.     virtual TermInfo*    MergeTermInfo(TermInfo* i, TermInfo* addTi, TermIndex* addIndex, DocID base);
  190.     virtual void        MergeDocIDs(TermIndex* addIndex, DocID base);
  191.     virtual uint32        MergeTermCost();        // used for merge progress estimations
  192.     virtual uint32        MergeDocCost();            // used for merge progress estimations
  193.     //  . vector indices
  194.     virtual DocInfo*    MakeDocInfo(IADoc* doc, DocID docID);
  195.     virtual void        UpdateDocInfo(DocInfo* i, TFVector* vector);
  196.     virtual void        MergeDocInfo(DocInfo* i, DocInfo* addDi, TermIndex* addIndex, TermID* reMap);
  197.  
  198.     // update support
  199.     virtual void        FinishingUpdate();
  200.     virtual void        DeletingDoc(DocInfo* docInfo);
  201.     virtual void        DeletingPostings(IATerm* term, TermFreq delCount);
  202.     virtual void        FlushUpdates();
  203.     // delete doc clean up
  204.     virtual void        ResetPostings(TermInfo* terminfo);
  205.  
  206.     // Used by RankedQuery.  Default impl here retokenizes, optimized by VectorIndex.
  207.     virtual bool        GetTFMaps(IADoc** docs, uint32 nDocs, TFMap** tfMaps, Progress* p);
  208.     
  209.     // default constructor etc. so that this can be a virtual base class
  210.             TermIndex();
  211.     void    Constructing(IAStorage* s, IACorpus* c, IAAnalysis* a, uint32 t, IABlockID r);
  212.     IAOrderedStorableSet*    GetTermInfoSet() const {return termInfoSet;}
  213.     IAOrderedStorableSet*    GetiDTermMap() const {return iDTermMap;}
  214.     void SetUpdateCount(uint32 cnt) {updateCount = cnt;}
  215.     uint32 GetUpdateCount() const {return updateCount;}
  216.     
  217.     void                SetBytesForUpdate(uint32 bupdate) {bytesForUpdate = bupdate;} // amount of memory available
  218.     uint32                GetBytesForUpdate() const {return bytesForUpdate;}    
  219.     
  220. private:
  221.     void                MaybeFlushUpdates();
  222.     void                FlushTermUpdates(Progress* prog);
  223.     void                FlushDocUpdates(Progress* prog);
  224.  
  225.     void                MergeTermInfos(IAIndex** indices, DocID* bases, Progress* prog);
  226.     void                MergeDocInfos(IAIndex** indices, DocID* bases, Progress* prog);
  227.  
  228.     // The substance of the index.
  229.     IAOrderedStorableSet*    docInfoSet;
  230.     IAOrderedStorableSet*    iDDocMap;
  231.     IAOrderedStorableSet*    termInfoSet;
  232.     IAOrderedStorableSet*    iDTermMap;
  233.     IAOrderedStorableSet**    mergeMaps;                // NULL except while merging
  234.  
  235.     uint32                updateCount;
  236.     
  237.     // queued updates.
  238.     TermUpdateSet*        termUpdateSet;
  239.     DocUpdateSet*        docUpdateSet;
  240.     
  241.     // persistent slots -- written in root
  242.     IABlockID            docInfoSetRoot;
  243.     IABlockID            iDDocMapRoot;
  244.     IABlockID            termInfoSetRoot;
  245.     IABlockID            iDTermMapRoot;
  246.     DocID                maxDocID;
  247.     TermID                maxTermID;
  248.     uint32                mergeMapCount;                // NULL except while merging
  249.     uint32*                mergeMapRoots;                // NULL except while merging
  250.  
  251.     FlushProgressFn*    flushProgressFn;
  252.     void*                flushProgressData;
  253.     clock_t                flushProgressFreq;
  254.     
  255.     uint32                bytesForUpdate;            // amount of memory available
  256.  
  257.  
  258.                         TermIndex(TermIndex&);        // don't define a copy constructor
  259. };
  260.  
  261.  
  262. IAExceptionCode                InvalidDocID = 'VIID';
  263. IAExceptionCode                InvalidTermID = 'VIIT';
  264.  
  265.  
  266. //// IDTerm: used in iDTermMap to map from TermIDs back to Terms -- ordered by decreasing id
  267. class IDTerm : public IAOrderedStorable {
  268. public:
  269.                 IDTerm();                    // term = NULL;
  270.                 IDTerm(TermID i, IATerm* t) : id(i), term(t) {}
  271.                 ~IDTerm();                    // delete term;
  272.  
  273.     // IAOrderedStorable methods
  274.     IAStorable*    DeepCopy() const;
  275.     IABlockSize    StoreSize() const;
  276.     void        Store(IAOutputBlock* out) const;
  277.     IAStorable*    Restore(IAInputBlock* in) const;
  278.     bool        LessThan(const IAOrderedStorable* other) const;    // return id > ((IDTerm*)other)->id;
  279.     bool        Equal(const IAOrderedStorable* other) const;    // return id == ((IDTerm*)other)->id;
  280.     TermID        GetTermID() const {return id;}                        
  281.     IATerm*        GetTerm() const {return term;}
  282.     void        SetTerm(IATerm* t) {term = t;}
  283.     void        SetTermID(TermID ID){ id = ID;}
  284.  
  285. private:
  286.                 IDTerm(IDTerm&);            // don't define a copy constructor
  287.                 
  288.     TermID        id;                            // the id
  289.     IATerm*        term;                        // and corresponding term
  290.  
  291.  
  292. };
  293.  
  294.  
  295. //// IDDoc: used in iDDocMap to map from DocIDs back to docs -- ordered by decreasing id
  296. class IDDoc : public IAOrderedStorable {
  297. public:
  298.                 IDDoc();                    // doc = NULL;
  299.                 IDDoc(DocID i, IADoc* d) : id(i), doc(d) {}
  300.                 ~IDDoc();                    // delete doc;
  301.  
  302.  
  303.     // IAOrderedStorable methods
  304.     IAStorable*    DeepCopy() const;
  305.     IABlockSize    StoreSize() const;
  306.     void        Store(IAOutputBlock* out) const;
  307.     IAStorable*    Restore(IAInputBlock* in) const;
  308.     bool        LessThan(const IAOrderedStorable* other) const;    // return id > ((IDDoc*)other)->id;
  309.     bool        Equal(const IAOrderedStorable* other) const;    // return id == ((IDDoc*)other)->id;
  310.     DocID        GetDocID() const {return id;}                        
  311.     IADoc*        GetDocument() const {return doc;}
  312.     void        SetDocument(IADoc*        d) {doc = d;}
  313. private:
  314.                 IDDoc(IDDoc&);            // don't define a copy constructor
  315.     DocID         id;                            // the ID
  316.     IADoc*        doc;                        // the document
  317.  
  318. };
  319.  
  320. #pragma IA_END_EXPORTS
  321.  
  322. #pragma import reset
  323. #endif
  324.